<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
                xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:msxsl="urn:schemas-microsoft-com:xslt"
                xmlns:x="http://schemas.microsoft.com/xps/2005/06/resourcedictionary-key">

  <xsl:output method="xml" version="1.0" omit-xml-declaration="yes" indent="yes" encoding="utf-8"/>
  
  <xsl:include href="include/Slices.xslt"/>

  <xsl:key name="Artifact_By_ActivityName" match="TaskTail" use="ActivityName"/>
  <xsl:key name="Artifact_By_ArtifactGroup" match="TaskTail" use="ArtifactGroup"/>

  <xsl:template match="/">

    <FlowDocument       PageWidth="21cm"
      PagePadding="60,40,40,40"
      PageHeight="29.7cm"
      ColumnWidth="21 cm">

      <xsl:call-template name="StyleSettings"/>

      <xsl:call-template name="Header"/>

      <xsl:if test="count(/ProjectDataSet/TaskTail) = 0">
        <Paragraph>
          There are no tailed artifacts at this project.                    
        </Paragraph>
      </xsl:if>
   

      

      <!-- EACH BACKLOG ITEM -->
      <xsl:for-each select="/ProjectDataSet/Project/BacklogItem">
        
        <xsl:sort select="BacklogItemNumber" data-type="number"/>

        <xsl:variable name="backlogItemUId" select="BacklogItemUId"/>

        
        <xsl:if test="count(/ProjectDataSet/TaskTail[BacklogItemUId=$backlogItemUId]) &gt; 0">

          <Paragraph Style="{{StaticResource TitleParagraph}}">
            [<xsl:value-of select="BacklogItemNumber"/>]
            <xsl:value-of select="Title"/>
            (<xsl:value-of select="count(/ProjectDataSet/TaskTail[BacklogItemUId=$backlogItemUId])"/> artifacts)
          </Paragraph>

          <xsl:call-template name="ImpactedItemList">
            <xsl:with-param  name="backlogItemUId" select="$backlogItemUId"/>
          </xsl:call-template>





          <Table>
            <TableRowGroup>
              <TableRow Style="{{StaticResource HeaderRow}}">

                <xsl:for-each select="/ProjectDataSet/TaskTail[count(. | key('Artifact_By_ActivityName', ActivityName)[1]) = 1]">
                  <xsl:sort select="ActivityOrder" data-type="number"/>
                  <xsl:variable name="activityName" select="ActivityName"/>

                  <xsl:if test="count(/ProjectDataSet/TaskTail[BacklogItemUId=$backlogItemUId and ActivityName=$activityName]) &gt; 0">
                    <TableCell>
                      <Paragraph>
                        <Bold>
                          <xsl:value-of select="ActivityName"/>
                        </Bold>
                      </Paragraph>
                    </TableCell>
                  </xsl:if>


                </xsl:for-each>
              </TableRow>

              <TableRow>
                <!-- EACH CYCLE ACTIVITY -->
                <xsl:for-each select="/ProjectDataSet/TaskTail[count(. | key('Artifact_By_ActivityName', ActivityName)[1]) = 1]">
                  <xsl:sort select="ActivityOrder" data-type="number"/>
                  <xsl:variable name="activityName" select="ActivityName"/>

                  <xsl:if test="count(/ProjectDataSet/TaskTail[BacklogItemUId=$backlogItemUId and ActivityName=$activityName]) &gt; 0">
                    <TableCell BorderThickness="0">

                      <xsl:for-each select="/ProjectDataSet/TaskTail[count(. | key('Artifact_By_ArtifactGroup', ArtifactGroup)[1]) = 1]">
                        <xsl:variable name="artifactGroup" select="ArtifactGroup"/>




                        <!-- EACH ARTIFACT -->
                        <xsl:call-template name="ArtifactList">
                          <xsl:with-param  name="backlogItemUId" select="$backlogItemUId"/>
                          <xsl:with-param  name="artifactGroup" select="$artifactGroup"/>
                          <xsl:with-param  name="activityName" select="$activityName"/>
                        </xsl:call-template>



                      </xsl:for-each>


                      <!-- EACH ARTIFACT -->
                      <xsl:call-template name="ArtifactListNoGroup">
                        <xsl:with-param  name="backlogItemUId" select="$backlogItemUId"/>
                        <xsl:with-param  name="activityName" select="$activityName"/>
                      </xsl:call-template>





                    </TableCell>
                  </xsl:if>

                </xsl:for-each>

              </TableRow>


            </TableRowGroup>
          </Table>



        </xsl:if>

       


      </xsl:for-each>


    </FlowDocument>
  </xsl:template>


</xsl:stylesheet>